<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/28
 * Time: 9:09
 */
namespace app\web\controller;

use app\web\service\BasicSettingService;

class BasicSetting extends Base {
    /**
     * @todo 获取基础配置
     * @author ZJF
     */
    public function getConfig(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $basic_service = new BasicSettingService();
        $res = $basic_service->getConfig($shop_id);
        if(!empty($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 设置基础配置
     * @author ZJF
     */
    public function editConfig(){
        $user=request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $order_sort = request()->param("order_sort/d",0);//订单排序
        $express_sort = request()->param("express_sort/d",0);//快递单宝贝打印顺序
        $invoice_sort = request()->param("invoice_sort/d",0);//发货单宝贝打印顺序
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($order_sort) && empty($express_sort) && empty($invoice_sort)){
            return json_ok([],1,'无效的设置');
        }
        $basic_model = new BasicSettingService();
        $param = array(
            'order_sort'=>$order_sort,
            'express_sort'=>$express_sort,
            'invoice_sort'=>$invoice_sort
        );
        $res = $basic_model->editConfig($shop_id,$param);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 新增/编辑快递单发件人
     * @author ZJF
     * @return \think\response\Json
     */
    public function editAddressExpress(){
        $user = request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $id = request()->param("id",0);//发件人信息ID
        $name = request()->param("name",'');//发件人姓名
        $name = trim($name);
        $phone = request()->param("phone",'');//发件人手机号
        $sign_name = request()->param("sign_name",'');//发件签名
        $sign_name = empty($sign_name)?$name:$sign_name;
        $post_code = request()->param("post_code/d",0);//邮编
        $province = request()->param("province",'');//省
        $province_code = request()->param("province_code/d",0);//省-行政编码
        $city = request()->param("city",'');//市
        $city_code = request()->param("city_code/d",0);//市-行政编码
        $district = request()->param("district",'');//区
        $district_code = request()->param("district_code/d",0);//区-行政编码
        $address = request()->param("address",'');//详细地址
        $address = trim($address);
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($name)){
            return json_ok([],1,'无效的发件人姓名');
        }
        if(empty($phone) || !checkPhone($phone)){
            return json_ok([],1,'无效的发件人电话');
        }
        if(empty($post_code) || !is_numeric($post_code)){
            return json_ok([],1,'无效的邮编');
        }
        if(empty($province)){
            return json_ok([],1,'无效的省');
        }
        if(empty($province_code) || !is_numeric($province_code)){
            return json_ok([],1,'无效的省-行政编码');
        }
        if(empty($city)){
            return json_ok([],1,'无效的市');
        }
        if(empty($city_code) || !is_numeric($city_code)){
            return json_ok([],1,'无效的市-行政编码');
        }
        if(empty($district)){
            return json_ok([],1,'无效的区');
        }
        if(empty($district_code) || !is_numeric($district_code)){
            return json_ok([],1,'无效的区-行政编码');
        }
        if(empty($address)){
            return json_ok([],1,'无效的详细地址');
        }
        $basic_model = new BasicSettingService();
        $param = array(
            'id'=>$id,
            'name'=>$name,
            'phone'=>$phone,
            'sign_name'=>$sign_name,
            'post_code'=>$post_code,
            'province'=>$province,
            'province_code'=>$province_code,
            'city'=>$city,
            'city_code'=>$city_code,
            'district'=>$district,
            'district_code'=>$district_code,
            'address'=>$address
        );
        $res = $basic_model->editAddressExpress($shop_id,$param);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 新增/编辑发货单发件人信息
     * @author ZJF
     * @return \think\response\Json
     */
    public function editAddressInvoice(){
        $user = request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        $id = request()->param("id",0);//发件人信息ID
        $title = request()->param("title",'');//发货标题
        $title = trim($title);
        $operator = request()->param("operator",'');//经办人
        $operator = trim($operator);
        $shop_name = request()->param("shop_name",'');//店铺名称
        $shop_name = trim($shop_name);
        $phone = request()->param("phone",'');//联系电话
        $tips = request()->param("tips",'');//友情提示
        $tips = trim($tips);
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        if(empty($title)){
            return json_ok([],1,'无效的发货标题');
        }
        if(empty($operator)){
            return json_ok([],1,'无效的经办人');
        }
        if(empty($shop_name)){
            return json_ok([],1,'无效的店铺名称');
        }
        if(empty($phone) || !checkPhone($phone)){
            return json_ok([],1,'无效的联系电话');
        }
        $basic_model = new BasicSettingService();
        $param = array(
            'id'=>$id,
            'title'=>$title,
            'phone'=>$phone,
            'operator'=>$operator,
            'shop_name'=>$shop_name,
            'tips'=>$tips
        );
        $res = $basic_model->editAddressInvoice($shop_id,$param);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 快递单发件人列表
     * @author
     */
    public function getAddressExpress(){
        $user = request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $basic_model = new BasicSettingService();
        $res = $basic_model->getAddressExpress($shop_id);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }

    /**
     * @todo 发货单发件人列表
     * @author
     */
    public function getAddressInvoice(){
        $user = request()->user;//登录信息
        $shop_id = empty($user['id'])?0:$user['id'];//店铺ID
        if(empty($shop_id) || !is_numeric($shop_id)){
            return json_ok([],1,'无效的店铺ID');
        }
        $basic_model = new BasicSettingService();
        $res = $basic_model->getAddressInvoice($shop_id);
        if(isset($res['code'])){
            return json_ok($res['data'],$res['code'],$res['message']);
        }
        return json_ok($res);
    }
}